﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bank.Operations;
using Bank.Account;
using Bank.Reports;


namespace Bank
{
    public class AccountManager
    {   

        public static AccountCollection accounts;

        public AccountManager()
        {
            accounts = new AccountCollection();
        }      

        public void makePayment(decimal amount,String accountID, int clientID)
        {
            IAccount account = accounts.GetAccountByID(accountID, clientID);           
            account.Ballance += amount;
        }


        public void makePayment(Money amount, String accountID, int clientID)
        {
            IAccount account = accounts.GetAccountByID(accountID, clientID);
            account.Ballance2 += amount;
        }

        public void makeOperation(int clientID , String accountID, IOperation operation)
        {
            IAccount account =  accounts.GetAccountByID(accountID, clientID);
            if (account != null)
            {
                if (account.Operationlist.Exists(o => o.Equals(operation.GetOperationName())))
                {
                    operation.ExecuteOperation(account);
                }              
            }
            else
            {
                throw new Exception("This account dose not exsist");

            }

            //IOperation op = account.Operationlist.OfType<operation.GetType()>();
             
        }

        public void makeOperation2(int clientID, String accountID, IOperation operation)
        {
            IAccount account = accounts.GetAccountByID(accountID, clientID);
            if (account != null)
            {
                if (account.Operationlist.Exists(o => o.Equals(operation.GetOperationName())))
                {
                    operation.ExecuteOperation2(account);
                }
            }
            else
            {
                throw new Exception("This account dose not exsist");

            }

            //IOperation op = account.Operationlist.OfType<operation.GetType()>();

        }

        public void registerOperation(int clientID, String accountID, IOperation operation)
        {
            IAccount account = accounts.GetAccountByID(accountID, clientID);
            if (!account.Operationlist.Exists(o => o.Equals(operation.GetOperationName())))
                account.Operationlist.Add(operation.GetOperationName());
        }

        public void deRegisterOperation(int clientID, String accountID, IOperation operation)
        {
            IAccount account = accounts.GetAccountByID(accountID, clientID);
            if (account.Operationlist.Exists(o => o.Equals(operation.GetOperationName())))
                account.Operationlist.Remove(operation.GetOperationName());
        }

        public void changeInterestCalculator(Int32 clientID , String accountID, IInterestCalculator icalculator)
        {
            IAccount account = getAccount(clientID, accountID);
            account.InterestCalculator = icalculator;

        }

        


        public IAccount createNewAccount(AccountType type, Int32 clienID, decimal ballance)
        {   
            IAccount newAccount;

            if (type == AccountType.CurrantAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else if (type == AccountType.SecondaryAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else if (type == AccountType.DepositAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else if (type == AccountType.CurrantAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else if (type == AccountType.SavingsAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else if (type == AccountType.ForeignCurrencyAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else 
                newAccount = null;

            return newAccount;
        }


        public IAccount createNewAccount(AccountType type, Int32 clienID, Money ballance)
        {
            IAccount newAccount;

            if (type == AccountType.CurrantAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else if (type == AccountType.SecondaryAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else if (type == AccountType.DepositAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else if (type == AccountType.CurrantAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else if (type == AccountType.SavingsAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else if (type == AccountType.ForeignCurrencyAccount)
            {
                newAccount = accounts.CreateCurrentAccount(clienID, ballance);
            }
            else
                newAccount = null;

            return newAccount;
        }       

        public IAccount getAccount(Int32 clientID , String accountID)
        {
            return accounts.GetAccountByID(accountID,clientID);           
        }

        public List<IAccount> getAllAccounts(List<String> accountNumber,Int32 clientID)
        {
            return accounts.GetAccountsByIDs(accountNumber, clientID);
        }

        public void DoMonthlyReport()
        {
            MonthlyReport mr = new MonthlyReport();
            foreach (IAccount ac in accounts.AccountList)
            {
                ac.Accept(mr);
            }
        }

        public void DoMonthlyReport2()
        {
            MonthlyReport mr = new MonthlyReport();
            foreach (IAccount ac in accounts.AccountList)
            {
                ac.Accept2(mr);
            }
        }

        public void DoYearReport()
        {
            YearReport yr = new YearReport();
            foreach (IAccount ac in accounts.AccountList)
            {
                ac.Accept(yr);
            }
        }


        public void DoYearReport2()
        {
            YearReport yr = new YearReport();
            foreach (IAccount ac in accounts.AccountList)
            {
                ac.Accept2(yr);
            }
        }


    }
}
